package jp.sourceforge.akjrcp.flvtools.explorer;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import jp.sourceforge.akjrcp.akj_betas.Akj_betasPlugin;

import org.eclipse.jface.window.Window;
import org.eclipse.swt.SWT;
import org.eclipse.swt.browser.Browser;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.ScrollBar;
import org.eclipse.swt.widgets.Shell;

public class FlowPlayerShell{
public static boolean firstTime=true;
	
private Browser browser;
private Shell shell;
File tmpFile;
public static String flowPlayerDirPath=System.getProperty("user.dir")+"\\"+"flowplayer";//should i move?
	public FlowPlayerShell(Shell parent){
		shell = new Shell(parent);
		shell.setLayout(new FillLayout());
		browser=new Browser(shell,SWT.NONE); 
		shell.setSize(364,326);
		if(firstTime){
		openFLVFile(new File(""));//for cash html files.
		try {
			Thread.sleep(1500);
		} catch (InterruptedException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		firstTime=false;
		}
		shell.open();
		shell.addDisposeListener(new DisposeListener(){

			public void widgetDisposed(DisposeEvent e) {
				removeTmpFile();
			}
			
		});
	}
	public void removeTmpFile(){
		if(tmpFile!=null){
			boolean result=tmpFile.delete();
			ExplorerView.log.debug("delete:"+result);
		}
	}
	public Shell getShell(){
		return shell;
	}
	
	public void openFLVFile(File file){
		try {
			shell.setText(file.getName());
			File tmp=File.createTempFile("flowplayer",".html");
			FileWriter writer=new FileWriter(tmp);
			FlowPlayer player=new FlowPlayer(file);
			writer.write(player.toHTML());
			writer.close();
			browser.setUrl("file://"+tmp.getAbsolutePath());
			removeTmpFile();//remove old one.
			tmpFile=tmp;
			ExplorerView.log.info(tmpFile);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public class FlowPlayer{
		private int width=360;
		private int height=240;
		private String headerHtml="<html><body style='marign=0;padding=0'><div align='center'>";
		private String footerHtml="</div></body></html>";
		private File file;
		public FlowPlayer(File file){
			this.file=file;
			//this.file=new File("c:\\tmp\\movie.flv");
		}
		public String toHTML(){
			StringBuffer str=new StringBuffer();
			String swfPath=flowPlayerDirPath+"\\"+"FlowPlayer.swf";
			String dir=file.getParent();
			String video=file.getName();
			str.append(headerHtml);
			str.append("<object type=\"application/x-shockwave-flash\" data=\""+swfPath+"\" width=\"320\" height=\"263\" id=\"FlowPlayer\">");
			str.append("<param name=\"allowScriptAccess\" value=\"sameDomain\" />");
					str.append("<param name=\"movie\" value=\""+swfPath+"\" />");
							str.append("<param name=\"quality\" value=\"high\" />");
									str.append("<param name=\"scale\" value=\"noScale\" />");
											str.append("<param name=\"wmode\" value=\"transparent\" />");
											str.append("<param name=\"flashvars\" value=\"baseURL="+dir+"&amp;videoFile="+video+"&amp;autoPlay=true&amp;bufferLength=5&amp;loop=false&amp;progressBarColor1=0xAAAAAA&amp;progressBarColor2=0x555555&amp;autoBuffering=false&amp;splashImageFile=main_clickToPlay.jpg&amp;hideControls=false\" />");
										str.append("</object>");
			
			str.append(footerHtml);
			//log.info(str.toString());
			return str.toString();
		}
	}
}
